package app.controller;

import app.dao.DeviceDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator 用户
 */
@Controller
@RequestMapping("/device")
public class DeviceController extends BaseController {

	@Resource
	private DeviceDao deviceDao;

	@RequestMapping("/import_excel")
	public String import_excel(@RequestParam("file") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) {
		JSONObject rtn = new JSONObject();
		try {
			if (!file.isEmpty()) {
				HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
				HSSFSheet hssfSheet = workbook.getSheetAt(0);
				for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
					HSSFRow hssfRow = hssfSheet.getRow(rowNum);
					HSSFCell c1 = hssfRow.getCell(0);
					HSSFCell c2 = hssfRow.getCell(1);
					HSSFCell c3 = hssfRow.getCell(2);
					HSSFCell c4 = hssfRow.getCell(3);
					c1.setCellType(CellType.STRING);
					c2.setCellType(CellType.STRING);
					c3.setCellType(CellType.STRING);
					c4.setCellType(CellType.STRING);
					String area = c1.getStringCellValue();
					String name = c2.getStringCellValue();
					String id = c3.getStringCellValue();
					String position = c4.getStringCellValue();
					String sql = "insert into device(id,name,userid,area,position) values('" + id + "','" + name + "','01','" + area + "','" + position + "')";
					deviceDao.execute(sql);
				}
				System.out.println(hssfSheet.getSheetName());
				workbook.close();
				// String fileRelName = file.getOriginalFilename();
				// String fileType = fileRelName.substring(fileRelName.lastIndexOf("."),
				// fileRelName.length());
			}
			rtn.put("rtn", 1);
			outJson(response, rtn.toString());
		} catch (Exception e) {
			rtn.put("rtn", 0);
			outJson(response, rtn.toString());
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/export_excel")
	public String export_excel(HttpServletRequest request, HttpServletResponse response) {
		JSONObject rtn = new JSONObject();
		try {
			String sql = "select * from device";
			JSONArray datas = deviceDao.listBase(sql);

			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet hssfSheet = workbook.createSheet();
			HSSFRow row0 = hssfSheet.createRow(0);
			HSSFCell r1=row0.createCell(0);
			HSSFCell r2=row0.createCell(1);
			HSSFCell r3=row0.createCell(2);
			HSSFCell r4=row0.createCell(3);
			r1.setCellType(CellType.STRING);
			r2.setCellType(CellType.STRING);
			r3.setCellType(CellType.STRING);
			r4.setCellType(CellType.STRING);
			r1.setCellValue("区县");
			r2.setCellValue("站点名称");
			r3.setCellValue("设备编号");
			r4.setCellValue("设备位置");
			for (int i = 0; i < datas.size(); i++) {
				JSONObject obj = datas.getJSONObject(i);
				String name = obj.getString("name");
				String id = obj.getString("id");
				String area = obj.getString("area");
				String position = obj.getString("position");
				HSSFRow hssfRow = hssfSheet.createRow(i+1);
				HSSFCell c1 = hssfRow.createCell(0);
				HSSFCell c2 = hssfRow.createCell(1);
				HSSFCell c3 = hssfRow.createCell(2);
				HSSFCell c4 = hssfRow.createCell(3);
				c1.setCellType(CellType.STRING);
				c2.setCellType(CellType.STRING);
				c3.setCellType(CellType.STRING);
				c4.setCellType(CellType.STRING);
				c1.setCellValue(area);
				c2.setCellValue(name);
				c3.setCellValue(id);
				c4.setCellValue(position);
			}

			// 八进制输出流 准备将Excel的输出流通过response输出到页面下载
			response.setContentType("application/octet-stream");
			// 这后面可以设置导出Excel的名称，此例中名为student.xls
			response.setHeader("Content-disposition", "attachment;filename=device.xls");
			// 刷新缓冲
			response.flushBuffer();
			// workbook将Excel写入到response的输出流中，供页面下载
			workbook.write(response.getOutputStream());

			workbook.close();
			rtn.put("rtn", 1);
			outJson(response, rtn.toString());
		} catch (Exception e) {
			rtn.put("rtn", 0);
			outJson(response, rtn.toString());
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/list")
	public String toIndex(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			outJson(response, deviceDao.list(data).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/login")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		try {
			// String data=request.getParameter("data");
			String data = request.getParameter("data");
			JSONObject user = deviceDao.login(data);
			if (!user.isEmpty()) {
				String userName = user.getString("name");
				String userId = user.getString("id");
				request.getSession().setAttribute("user", user.toString());
				request.getSession().setAttribute("userName", userName);
				request.getSession().setAttribute("userId", userId);
				request.getSession().setAttribute("positionInfo", "");

			}
			outJson(response, user.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/save")
	public String save(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.save(data, request.getSession().getAttribute("userName").toString()));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/getById")
	public String getById(HttpServletRequest request, HttpServletResponse response) {
		try {
			String id = request.getParameter("id").toString();
			JSONObject rtn = new JSONObject();
			rtn.put("data", deviceDao.getById(id));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/resetPassword")
	public String resetPassword(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.resetPassword(data));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/offOrOnUser")
	public String offOrOnUser(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.offOrOnUser(data));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/delByIds")
	public String delByIds(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.delByIds(data));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/userNameCheck")
	public String userNameCheck(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.userNameCheck(data));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/changeCurrentUserPassword")
	public String changeCurrentUserPassword(HttpServletRequest request, HttpServletResponse response) {
		try {
			String data = request.getParameter("data");
			JSONObject rtn = new JSONObject();
			rtn.put("rtn", deviceDao.changeCurrentUserPassword(data));
			outJson(response, rtn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
